package cn.xm.canting.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import cn.djp.canting.R;
import cn.xm.canting.adapter.OrderItemAdapter;
import cn.xm.canting.bean.AllFood;
import cn.xm.canting.bean.OrderListBean;
import cn.xm.canting.bean.UserOrder;

//订单列表
public class OrderActivity extends AppCompatActivity implements View.OnClickListener {


    private Button order_select;
    private ImageView user_head;
    private TextView user_name;
    private ListView order_listview;
    private final Context context = this;
    public List<OrderListBean> beans;
    private Intent intent;
    private String phone;
    private Button order_setting;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        initView();
    }

    private void initView() {
        order_select = (Button) findViewById(R.id.order_select);
        user_head = (ImageView) findViewById(R.id.user_head);
        user_name = (TextView) findViewById(R.id.user_name);
        order_setting = (Button) findViewById(R.id.order_setting);
        order_listview = (ListView) findViewById(R.id.order_listview);
        intent = getIntent();
        phone = intent.getStringExtra("phone");
        beans = new ArrayList<OrderListBean>();
        updateUi();
        order_select.setOnClickListener(this);
        order_setting.setOnClickListener(this);
        //为listview中的item设置点击事件
        order_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(context, OrderContentActivity.class);
                //将商品信息传递并跳转至商品详情界面
                intent.putExtra("name", beans.get(position).getFoodName());
                intent.putExtra("content", beans.get(position).getFoodContent());
                intent.putExtra("price", beans.get(position).getAllPrice());
                intent.putExtra("img", beans.get(position).getImgId());
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //点击"点餐"按钮跳转至点餐界面
            case R.id.order_select:
                Intent selectIntent = new Intent(this, SelectActivity.class);
                //传递用户手机号phone
                selectIntent.putExtra("phone", phone);
                startActivity(selectIntent);
                break;
            //点击"设置"按钮跳转至设置界面
            case R.id.order_setting:
                Intent settingIntent = new Intent(this, SettingActivity.class);
                settingIntent.putExtra("phone", phone);
                startActivity(settingIntent);
                break;
        }
    }

    //更新UI
    public void updateUi() {
        //更新用户名
        user_name.setText(phone + "");
        //更新头像
        user_head.setImageResource(R.mipmap.user_icon);
        //获取订单信息
        getData();
        //更新listview
        order_listview.setAdapter(new OrderItemAdapter(context, beans));
    }

    //获取到订单信息
    private void getData() {
        //通过手机号查询用户拥有的订单的信息
        List<UserOrder> userOrders = LitePal.where("phone = ?", phone).find(UserOrder.class);

        //获取商品的id和数量
        for (int i = 0; i < userOrders.size (); i++) {
            int orderId = userOrders.get(i).getOrderId();
            int num = userOrders.get(i).getNum();

            //通过商品id查询商品的名称,图标和价格
            List<AllFood> foods = LitePal.where("id = ?", String.valueOf(orderId)).find(AllFood.class);
            if (foods.size() != 0) {

                //beans是一个全局List集合,存放用户的订单并传递给adapter
                //这里将查询到的商品数据封装至beans
                beans.add(new OrderListBean(foods.get(0).getFoodName(),
                        foods.get(0).getFoodContent(),
                        foods.get(0).getFoodPrice() * num,
                        foods.get(0).getImgId()
                ));
            }
        }
    }
}

